package cloud.daodao.storm.common.util.security;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author DaoDao
 */
public class MaskUtil {

    public static String mask(MaskType type, String origin) {
        if (null == origin || origin.isEmpty()) {
            return origin;
        }
        String mask;
        if (type.equals(MaskType.NAME)) {
            mask = nameMask(origin);
            return mask;
        }
        Matcher matcher = Pattern.compile(type.getRegex()).matcher(origin);
        mask = matcher.replaceAll(type.getReplace());
        return mask;
    }

    private static String nameMask(String origin) {
        if (null == origin || origin.isEmpty()) {
            return origin;
        }
        int length = origin.length();
        String mask;
        if (1 == length) {
            mask = origin;
        } else if (2 == length) {
            mask = "*" + origin.charAt(length - 1);
        } else {
            mask = origin.charAt(0) + "*".repeat(Math.min(length - 2, 4)) + origin.charAt(length - 1);
        }
        return mask;
    }

    /**
     * 敏感类型枚举
     *
     * @author DaoDao
     */
    @Getter
    @AllArgsConstructor
    public enum MaskType {

        /**
         * 姓名，默认规则保留第一个字符，其余按照字符个数替换掩码 *
         * 大猫        : 大*
         * 大脸猫      : 大**
         * 大脸猫喵喵喵 : 大*****
         */
        // NAME("([\u4E00-\u9FA5]{1})([\u4E00-\u9FA5]{1})([\u4E00-\u9FA5]{0,})", "*$2$3"),
        NAME("(?<=.).", "*"),

        /**
         * 手机
         */
        PHONE("(1\\d{2})(\\d{4})(\\d{4})", "$1****$3"),

        /**
         * 电话
         */
        TEL("(\\d{3})(\\d{0,4})(\\d{4})", "$1****$3"),

        /**
         * 邮箱
         */
        EMAIL("([a-zA-Z0-9_-]{2})([a-zA-Z0-9_-]+)(@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+)", "$1****$3"),

        /**
         * 身份证
         */
        ID_CARD("(\\d{6})(\\d{5,8})(\\d{4}|\\d{3}[Xx]{1})", "$1****$3"),

        /**
         * 银行卡
         */
        BANK_CARD("(\\d{6})(\\d{2,22})(\\d{4})", "$1****$3"),

        /**
         * 证
         */
        LICENCE("^([\\s\\S]{6})([\\s\\S]*)([\\s\\S]{4})$", "$1****$3"),

        /**
         * 密码
         */
        PASSWORD("^[\\s\\S]*$", "******"),

        ;

        /**
         * 正则
         */
        final String regex;

        /**
         * 替换
         */
        final String replace;

    }

}
